var root_host = "https://api.yqdhz.com/";
import md5 from "./tool/md5/md5.js";
export default {
	root_host : "https://api.yqdhz.com/",
    config:{},
	authParam:function(param){
		let ts = Math.round(new Date().getTime()/1000).toString();
		let sign = ts.substr(-4);
		const authParam = {"ts":ts,"sign":md5.hex_md5(sign)}
		return Object.assign(param,authParam);
	},
	getUserToken:function(n){
		this.authParam({});
		// if(!uni.getStorageSync("uToken")){
		// 	const h = root_host;
		// 	uni.login({
		// 		provider: 'weixin', //使用微信登录
		// 		success: function (loginRes) {
		// 			const code = loginRes.code;
		// 			uni.request({
		// 				url: h+'api/fangji.index/user',
		// 				data: t.authParam({code:code,appName:n.appName}),
		// 				success: (res) => {
		// 					var reposeData = res.data;
		// 					const userData = reposeData.data;
		// 					uni.setStorageSync("uToken",userData.uuid)
		// 				}
		// 			});
		// 		}
		// 	});
		// }
		var uid = n.xCacheGet("uid");
		if(!uid){
			uid = Math.random().toString(36).substr(2);
			var timestamp = Date.now();
			n.xCacheSet("uid",uid);
			n.xCacheSet("utime",parseInt((timestamp/1000).toString()))
		}
		return uid;
	},
	getWall:function(t){
		t.x_request('api/wall/detail',{id:t.id},function(t,res){
			if (res.code === 1) {
				t.item = res.data;
			}
		});
	},
	showCommentWrap(t,comment_id,reply_id){
		t.showComment = !t.showComment;
		t.baseForm.comment_id = comment_id;
		t.baseForm.reply_id = reply_id;
	},
	// 获取评论列表
	getCommentList:function(t){
		if (t.commentObj.current_page >= t.commentObj.last_page) {
			t.noMore = 1;
			t.commentObj.message = "没有更多数据了";
		} else {
			t.x_request('api/comment',{id:t.id, page: t.commentObj.current_page + 1,},function(t,res){
				if (res.code === 1) {
					t.commentObj.list = t.commentObj.list.concat(res.data.data);
					t.commentObj.current_page = res.data.current_page;
					t.commentObj.last_page = res.data.last_page;
				}
			});
		}
	},
	getComment:function(t){
		t.x_request('api/comment/detail',{comment_id:t.comment_id},function(t,res){
			if (res.code === 1) {
				t.comment = res.data;
			}
		});
	},
	//提交评论
	addComment:function(t){
		t.x_request('api/comment/add',{id:t.id, comment_id:t.baseForm.comment_id, reply_id:t.baseForm.reply_id, content:t.baseForm.content},function(t,res){
			if (res.code === 1) {
				if(t.baseForm.comment_id === 0){
					t.commentObj.list = t.commentObj.list.concat(res.data);
				}else{
					t.replyObj.list = t.replyObj.list.concat(res.data);
				}
				t.cancelComment();
			}
		});
	},
	// 获取回复列表
	getReplyList:function(t){
		if (t.replyObj.current_page >= t.replyObj.last_page) {
			t.noMore = 1;
			t.replyObj.message = "没有更多数据了";
		} else {
			t.x_request('api/comment/replyList',{comment_id:t.comment_id, page: t.replyObj.current_page + 1,},function(t,res){
				if (res.code === 1) {
					t.replyObj.list = t.replyObj.list.concat(res.data.data);
					t.replyObj.current_page = res.data.current_page;
					t.replyObj.last_page = res.data.last_page;
				}
			});
		}
	},
	wallLike:function(t,itemId,type){
		t.x_request('api/wall/like',{item_id:itemId, type:type},function(t,res){
			if(res.code === 1){
				if (res.code === 1) {
					res = res.data;
					if(res.type == 1){
						t.item.is_like = res.type;
						t.item.like_num = res.count;
					}else{
						t.item.is_like = res.type;
						t.item.like_num = res.count;
					}
				}
			}
		});
	},
	getUserInfo: function(t){
		var userInfo = t.xCacheGet('UserInfo');
		if(!userInfo){
			var uid = t.xCacheGet('uid');
			userInfo = {
				uid:uid,
				nickName:uid,
				coins:0
			};
			t.xCacheSet('UserInfo',userInfo)
		}
		return userInfo;

	},
	setUserInfo: function(t,userInfo){
		t.xCacheSet('UserInfo',userInfo)
	},
	changeNickName: function(t){
		t.x_request('api/user/nickname',{nick_name:t.userInfo.nick_name},function(t,res){
			if(res.code === 0){
				uni.showToast({
					title: res.info,
					icon: 'none'
				})
			}else{
				t.userInfo.nick_name = res.data.nick_name;
				t.showNickNameEdit = false;
			}
		});
	},
	getMyMenu: function(t){
		t.x_request('api/user/menu',{},function(t,res){
			if(res.code === 1){
				t.menuList = res.data;
			}
		});
	},
	toAsk: function(t){
		uni.showLoading({
			title: '思考中...'
		});
		var tt = t;
		if(tt.isProduct){
			tt.x_alert('error',"响应时间比较久，观看视频等待一下，并免费领取奖励次数","dialogConfirm");
		}
		t.x_request('api/chat/ask',{ask:t.ask},function(t,res){
			if (res.code === 1) {
				t.ask = '';
				t.itemList = t.itemList.concat(res.data);
				t.toEnd();
			}else if(res.code === 10001){
				tt.x_alert('error',"今日提问次数已达到上限，观看免费领取提问次数","dialogConfirm");
			}
			uni.hideLoading();
		});
	}
}
